﻿using System.Collections.Generic;
using System.Linq;
using JetBrains.ReSharper.Daemon.CSharp.Stages;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Stages.Dispatcher;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using MugenMvvmToolkit.ReSharper.Highlighting;
using MugenMvvmToolkit.ReSharper.Infrastructure;

namespace MugenMvvmToolkit.ReSharper.ViewModels.Analyzers
{
    [ElementProblemAnalyzer(new[] { typeof(ICSharpLiteralExpression) },
        HighlightingTypes = new[] { typeof(ResolvedLiteralHighlighting), typeof(InvalidValidateMethodCallHighlighting) })]
    public class ValidatableViewModelValidateMethodAnalyzer : ElementProblemAnalyzerBase<ICSharpLiteralExpression>
    {
        #region Overrides of ElementProblemAnalyzerBase<ICSharpLiteralExpression>

        protected override void Run(ICSharpLiteralExpression element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var result = ToolkitExtensions.TryGetValidateMethod(element, TypeProvider.PsiModule, TypeProvider.ResolveContext);
            if (result == null)
                return;
            var names = new HashSet<string>(result.Item3.Select(property => property.ShortName));
            var s = element.ConstantValue.Value as string;
            if (string.IsNullOrEmpty(s) || names.Count == 0)
                return;
            if (names.Contains(s))
                consumer.AddHighlighting(new ResolvedLiteralHighlighting(element));
            else
                consumer.AddHighlighting(new InvalidValidateMethodCallHighlighting(result.Item2, result.Item1, names));
        }

        #endregion
    }
}